<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="username"
					type="text"
					placeholder="用户名/电话"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				text="登 录"
				
				@click.native="login"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<!-- <view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view> -->
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var api = require('@/common/api.js')
	// var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		onLoad(e) {
			if (e.token) {
				uni.showLoading()
				//console.log(e.token);
				uni.setStorageSync('upload', 1)
				uni.setStorageSync('login', 1)
				uni.setStorageSync('token', e.token)
				uni.showToast({
					duration: 1000,
					title: '登录成功'
				});
				setTimeout((e => {
					uni.hideLoading()
					uni.switchTab({
						url: '../user/user'
					});
				}), 1000);
			}
		},
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				loading: false,
				username: "",
				password: "",
			};
		},
		components:{
			wInput,
			wButton,
		},

		methods: {

		    login() {
		    	this.loading = true;
		    	if (this.username == '') {
		    		uni.showToast({
		    			icon: 'none',
		    			title: '请输入手机号或邮箱'
		    		});
		    		this.loading = false;
		    		return;
		    	}
		    	if (this.password == '') {
		    		uni.showToast({
		    			icon: 'none',
		    			title: '请输入密码'
		    		});
		    		this.loading = false;
		    		return;
		    	}
		    	api.post({
		    		url: 'user/public/login',
		    		data: {
		    			username: this.username,
		    			password: this.password,
		    			device_type: api.DeviceType
		    		},
		    		success: data => {
		    			//console.log(data);
		    			if (data.code == 1) {
		    				this.loading = false;
		    				//console.log(data);
		    				uni.showToast({
		    					duration: 500,
		    					title: data.msg
		    				});
		    				uni.setStorageSync('upload', 1)
		    				uni.setStorageSync('login', 1)
		    				uni.setStorageSync('token', data.data.token)
		    				uni.setStorageSync('user', data.data.user)
		    				setTimeout((e => {
		    					uni.switchTab({
		    						url:'../user/user'
		    					});
		    				}), 500);
		    			}
		    			if (data.code == 0) {
		    				this.loading = false;
		    				uni.showToast({
		    					duration: 1500,
		    					icon: 'none',
		    					title: data.msg
		    				});
		    			}
		    		}
		    	})
		    },
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
